import com.tc.jetbrick.HtmlToPdfUtil;
import com.tc.jetbrick.JetbrickUtil;
import jetbrick.template.JetTemplate;
import jxl.Workbook;
import jxl.format.CellFormat;
import jxl.write.Label;
import jxl.write.WritableCell;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;

import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class testJetbrick {

    @Test
    public void demo_pdf() throws Exception{ //pdf模板
        List<String> books=new ArrayList<>();
        books.add("java");
        books.add("python");
        Map<String, Object> context = new HashMap<>();
        context.put("name","田才");
        context.put("ager","18");
        context.put("books",books);
        InputStream productServiceIS = testJetbrick.class.getResourceAsStream("/template/pdf/pdf_tpl.html");
        JetTemplate jetTemplate = JetbrickUtil.getJetTemplate(IOUtils.toString(productServiceIS, "UTF-8"));
        String htmlSource = JetbrickUtil.render(jetTemplate, context);
        HtmlToPdfUtil.html2PdfByItext7(htmlSource, "d://abc__.pdf");
    }

    @Test
    public void demo_excel() throws Exception{//excel模板
        Workbook rw = Workbook.getWorkbook(testJetbrick.class.getResourceAsStream("/template/excel/pycredit.xls"));
        //这一步在原有文件的基础上新建一个文件
        WritableWorkbook ww = Workbook.createWorkbook(new File("d://abc.xls"),rw);

        //获得编号为2的sheet
        WritableSheet sheet1 = ww.getSheet(0);
        //添加
        sheet1.addCell(getLable(2,0,"田才有限公司",sheet1));//查询机构。
        sheet1.addCell(getLable(7,0,"今天",sheet1));//查询时间
        ww.write();
        ww.close();
        rw.close();
    }

    public Label getLable(int i, int j, String s, WritableSheet sheet1){
        if(StringUtils.isBlank(s)) s="无";
        WritableCell cell=sheet1.getWritableCell(i, j);
        //获取上一部cell的格式
        CellFormat cf=cell.getCellFormat();
        Label lable=new Label(i, j, s);
        //将修改后的单元格格式设置成和原来一样的
        lable.setCellFormat(cf);
        return lable;
    }

}
